package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.core.BaseServlet;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Course;
import com.itheima.service.store.CatalogService;
import com.itheima.service.store.CourseService;
import com.itheima.service.store.impl.CatalogServiceImpl;
import com.itheima.service.store.impl.CourseServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {
    private CatalogService catalogService = new CatalogServiceImpl();
    private CourseService courseService = new CourseServiceImpl();

    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /*1.获取表单数据*/
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if (StringUtils.isBlank(pageNum)) {
            pageNum = "1";
        }
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }

        /*2.调用Service*/
        PageInfo<Catalog> pageInfo = catalogService.queryPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));

        /*3.回显数据*/
        request.setAttribute("page",pageInfo);
        return "/WEB-INF/pages/store/catalog/list.jsp";
    }

    /**
     * 跳转到新建add.jsp页面
     */
    public String toAdd(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Course> list = courseService.findAll();
        request.setAttribute("courseList",list);
        return "/WEB-INF/pages/store/catalog/add.jsp";
    }

    /**
     * 保存，保存完毕后跳转到列表页面
     */
    public String save(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class,"yyyy-MM-dd");
        catalogService.save(catalog);
        return "/store/catalog/list";
    }

    /**
     * 根据ID回显数据
     */
    public String toEdit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");
        /*查询当前部门信息*/
        Catalog catalog = catalogService.findById(id);
        request.setAttribute("catalog",catalog);

        List<Catalog> courseList = catalogService.findAll();
        request.setAttribute("courseList",courseList);
        return "/WEB-INF/pages/store/catalog/update.jsp";
    }


    /**
     * 更新数据，更新完毕后跳转到列表页面
     */
    public String edit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class,"yyyy-MM-dd");
        catalogService.update(catalog);
        return "/store/catalog/list";
    }

    /**
     * 删除，删除完毕跳转到列表页面
     */
    public String delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String[] ids = request.getParameterValues("id");
        catalogService.deleteById(ids);
        return "/store/catalog/list";
    }

}
